package likou.tanxin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Info:
 * @Author: Tangxz
 * @Date: 2020/09/07 07:31
 */
public class _347 {
    public static void main(String[] args) {
        int[] daan = topKFrequent(new int[]{1,1,1,2,2,3},2);
        for (int i :daan){
            System.out.println(i);
        }
    }
    public static int[] topKFrequent(int[] nums, int k) {
        List<String> list = new ArrayList<>();
        Map<Integer,Integer> map = new HashMap<>();
        for(int i=0;i<nums.length;i++){
            if(!map.containsKey(nums[i])){
                map.put(nums[i],1);
            }else{
                map.put(nums[i],map.get(nums[i])+1);
            }
        }
        for(Integer key:map.keySet()){
            String now = key+"_"+map.get(key);
            list.add(now);
        }
        list = list.stream().sorted((s1, s2) -> {
            Integer i1 = Integer.valueOf(s1.split("_")[1]);
            Integer i2 = Integer.valueOf(s2.split("_")[1]);
            return i2 - i1;
        }).collect(Collectors.toList());
        int[] daan = new int[k];
        for (int i=0;i<k;i++){
            daan[i] = Integer.valueOf(list.get(i).split("_")[0]);
        }
        List<Integer> daan2 = new ArrayList<>();
        Integer[] ww = new Integer[daan2.size()];
        daan2.toArray(ww);
        return daan;
    }
}
